<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="500"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			:model="form"
			:rules="rules"
			:disabled="mode == 'show'"
			ref="form"
			label-width="100px"
		>
			<el-form-item label="菜单名称" prop="title">
				<el-input
					v-model="form.title"
					placeholder="请输入菜单名称"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="菜单code" prop="menuCode">
				<el-input
					v-model="form.menuCode"
					placeholder="请输入菜单code"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="菜单图标" prop="icon">
				<sc-icon-select v-model="form.icon"></sc-icon-select>
				<span style="margin-left: 16px">{{ form.icon }}</span>
			</el-form-item>
			<el-form-item label="父菜单" prop="parentId">
				<MenuTreeSelect
					v-model="form.parentId"
					placeholder="请选择父菜单"
				>
				</MenuTreeSelect>
			</el-form-item>

			<el-form-item label="路由地址" prop="path">
				<el-input
					v-model="form.path"
					placeholder="请输入路由地址"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="组件路径" prop="component">
				<el-input
					v-model="form.component"
					placeholder="请输入组件路径"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="是否显示" prop="visible">
				<el-switch v-model="form.visible"></el-switch>
			</el-form-item>
			<el-form-item label="菜单排序" prop="sort">
				<el-input-number v-model="form.sort" :min="0"></el-input-number>
			</el-form-item>
			<el-form-item label="开启缓存" prop="isCache">
				<el-switch v-model="form.isCache"></el-switch>
			</el-form-item>
			<el-form-item label="是否开启" prop="status">
				<el-switch v-model="form.status"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button
				v-if="mode != 'show'"
				type="primary"
				:loading="isSaveing"
				@click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import MenuTreeSelect from "./MenuTreeSelect.vue";
import scIconSelect from "@/components/scIconSelect";
export default {
	components: {
		MenuTreeSelect,
		scIconSelect,
	},
	props: {
		show: {
			type: Boolean,
		},
	},
	emits: ["success", "closed"],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
				show: "查看",
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				menuCode: "",
				title: "",
				icon: "",
				path: "",
				component: "",
				isCache: true,
				sort: 0,
				visible: true,
				status: true,
				parentId: 0,
			},
			//验证规则
			rules: {
				menuCode: [{ required: true, message: "请输入菜单code" }],
				title: [{ required: true, message: "请输入菜单标题" }],
			},
		};
	},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		submit() {
			this.$refs.form.validate((valid) => {
				if (valid) {
					this.$API.menu.add(this.form).then((data) => {
						if (data.code == 200) {
							this.$message.success("添加成功");
							this.visible = false;
						}
					});
				}
			});
		},
	},
};
</script>

<style></style>
